package com.feinno.urcs.group.interop.rpcclient;

import com.feinno.rpc.channel.RpcFuture;
import com.feinno.rpc.client.RpcMethodStub;
import com.feinno.urcs.group.entity.PSResult;
import com.feinno.urcs.group.entity.SubscribeImpsArgs;
import com.feinno.urcs.group.interop.IUserIndexService;
import com.feinno.urcs.group.interop.entity.FXCatalogIndex;
import com.feinno.urcs.group.interop.entity.FXMobileNo;
import org.helium.framework.annotations.FieldSetter;
import org.helium.framework.annotations.ServiceImplementation;
import org.helium.framework.rpc.LegacyRpcClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Li.Hongbo <lihongbo@feinno.com>
 */
@ServiceImplementation
public class IUserIndexServiceImpl implements IUserIndexService {

	private static final Logger LOGGER = LoggerFactory.getLogger(IUserIndexServiceImpl.class);
	@FieldSetter("${FX_CATS_RPC_SERVICE}")
	private LegacyRpcClient rpcClient;
	@FieldSetter("${FX_PS_RPC_SERVICE}")
	private LegacyRpcClient ps;


	@Override
	public FXCatalogIndex getUserIndexByMobileNo(FXMobileNo mobileNo) throws Exception {
		RpcMethodStub methodStub = rpcClient.getMethodStub("GetUserIndexByMobileNo");
		RpcFuture future = methodStub.invoke(mobileNo);
		future.setTimeout(2000);
		FXCatalogIndex fxCatalogIndex = future.syncGet(FXCatalogIndex.class);
		if (fxCatalogIndex == null) {
			fxCatalogIndex = subImps(mobileNo);
		}
		return fxCatalogIndex;

	}

	private FXCatalogIndex subImps(FXMobileNo mobileNo) {
		try {
			RpcMethodStub methodStub = ps.getMethodStub("SubscribeImps");
			SubscribeImpsArgs subscribeImpsArgs = new SubscribeImpsArgs();
			subscribeImpsArgs.setMobileNo(mobileNo);
			subscribeImpsArgs.setNickName("飞信用户");
			subscribeImpsArgs.setRequestSourceId((short) 24);
			RpcFuture future = methodStub.invoke(subscribeImpsArgs);
			future.setTimeout(2000);
			PSResult ret = future.syncGet(PSResult.class);
			if (ret.getCode() == 0) {
				return getUserIndexByMobileNo(mobileNo);
			}
			return null;
		} catch (Exception e) {
			LOGGER.warn("SubscribeImps failed, mobileNo : {}", mobileNo);
			return null;
		}
	}

	@Override
	public FXCatalogIndex getUserIndexBySid(int sid) throws Exception {
		RpcMethodStub methodStub = rpcClient.getMethodStub("GetUserIndexBySid");
		RpcFuture future = methodStub.invoke(sid);
		future.setTimeout(2000);
		return future.syncGet(FXCatalogIndex.class);
	}

	@Override
	public FXCatalogIndex getUserIndexByUserId(int uid) throws Exception {
		RpcMethodStub methodStub = rpcClient.getMethodStub("GetUserIndexByUserId");
		RpcFuture future = methodStub.invoke(uid);
		future.setTimeout(2000);
		return future.syncGet(FXCatalogIndex.class);
	}

	@Override
	public FXCatalogIndex getUserIndexByEmail(String email) throws Exception {
		RpcMethodStub methodStub = rpcClient.getMethodStub("GetUserIndexByEmail");
		RpcFuture future = methodStub.invoke(email);
		future.setTimeout(2000);
		return future.syncGet(FXCatalogIndex.class);
	}

	@Override
	public int getOriginalSidByMobileNo(FXMobileNo mobileNo) {
		throw new UnsupportedOperationException("Unsupported GetOriginalSid");
//		RpcMethodStub methodStub = rpcClient.getMethodStub("GetOriginalSid");
//		RpcFuture future = methodStub.invoke(mobileNo);
//		future.setTimeout(1000);
//		return future.syncGet(FXCatalogIndex.class);
	}
}
